#include "TCPClient.h"

using namespace boost::asio;

void TCPClient::startReceive()
{
    boost::asio::async_read(socket, receivebuffer, boost::asio::transfer_at_least(1),
		[this](const boost::system::error_code& ec, std::size_t bytes_transferred) 
		{
		if (!ec) {
			std::cout << "Raw buffer content: ";
			std::istreambuf_iterator<char> eos;
			std::string bufferContent(std::istreambuf_iterator<char>(&receivebuffer), eos);
			std::cout << bufferContent << std::endl;
			startReceive(); 
		}
		else {
			std::cout << "Error receiving data: " << ec.message() << std::endl;
		}
		});
	boost::thread ioThread([this]() { io.run(); });
	ioThread.detach();
}

void TCPClient::send(const std::string& mes)
{
	boost::asio::write(socket, boost::asio::buffer(mes));
}